package com.vogue.meeting.common.security.exception;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.vogue.meeting.common.web.domain.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.io.IOException;

/**
 * 异常结果序列化
 */
public class CustomOauthExceptionSerializer extends StdSerializer<CustomOauthException> {
    private static final Logger log = LoggerFactory.getLogger(CustomOauthExceptionSerializer.class);
    public CustomOauthExceptionSerializer() {
        super(CustomOauthException.class);
    }

    @Override
    public void serialize(CustomOauthException value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        log.debug("自定义返回格式内容: "+ Jackson2ObjectMapperBuilder.json().build().writeValueAsString(Result.failure(value.getHttpErrorCode(),value.getOAuth2ErrorCode()+":"+value.getMessage())));
        // 自定义返回格式内容
        gen.writeObject(Result.failure(value.getHttpErrorCode(),value.getOAuth2ErrorCode()+":"+value.getMessage()));
    }
}
